/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package it.apuliait.service.persistence;

import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.UnmodifiableList;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.CacheModel;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import it.apuliait.NoSuchLineaFatturaException;

import it.apuliait.model.LineaFattura;
import it.apuliait.model.impl.LineaFatturaImpl;
import it.apuliait.model.impl.LineaFatturaModelImpl;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the linea fattura service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Donatello
 * @see LineaFatturaPersistence
 * @see LineaFatturaUtil
 * @generated
 */
public class LineaFatturaPersistenceImpl extends BasePersistenceImpl<LineaFattura>
	implements LineaFatturaPersistence {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never modify or reference this class directly. Always use {@link LineaFatturaUtil} to access the linea fattura persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
	 */
	public static final String FINDER_CLASS_NAME_ENTITY = LineaFatturaImpl.class.getName();
	public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
		".List1";
	public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
		".List2";
	public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(LineaFatturaModelImpl.ENTITY_CACHE_ENABLED,
			LineaFatturaModelImpl.FINDER_CACHE_ENABLED, LineaFatturaImpl.class,
			FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
	public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(LineaFatturaModelImpl.ENTITY_CACHE_ENABLED,
			LineaFatturaModelImpl.FINDER_CACHE_ENABLED, LineaFatturaImpl.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
	public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(LineaFatturaModelImpl.ENTITY_CACHE_ENABLED,
			LineaFatturaModelImpl.FINDER_CACHE_ENABLED, Long.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
	public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_COMPETENZE =
		new FinderPath(LineaFatturaModelImpl.ENTITY_CACHE_ENABLED,
			LineaFatturaModelImpl.FINDER_CACHE_ENABLED, LineaFatturaImpl.class,
			FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByCompetenze",
			new String[] {
				Long.class.getName(),
				
			Integer.class.getName(), Integer.class.getName(),
				OrderByComparator.class.getName()
			});
	public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPETENZE =
		new FinderPath(LineaFatturaModelImpl.ENTITY_CACHE_ENABLED,
			LineaFatturaModelImpl.FINDER_CACHE_ENABLED, LineaFatturaImpl.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByCompetenze",
			new String[] { Long.class.getName() },
			LineaFatturaModelImpl.DOCUMENTOIDFK_COLUMN_BITMASK);
	public static final FinderPath FINDER_PATH_COUNT_BY_COMPETENZE = new FinderPath(LineaFatturaModelImpl.ENTITY_CACHE_ENABLED,
			LineaFatturaModelImpl.FINDER_CACHE_ENABLED, Long.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByCompetenze",
			new String[] { Long.class.getName() });

	/**
	 * Returns all the linea fatturas where documentoIDFK = &#63;.
	 *
	 * @param documentoIDFK the documento i d f k
	 * @return the matching linea fatturas
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public List<LineaFattura> findByCompetenze(long documentoIDFK)
		throws SystemException {
		return findByCompetenze(documentoIDFK, QueryUtil.ALL_POS,
			QueryUtil.ALL_POS, null);
	}

	/**
	 * Returns a range of all the linea fatturas where documentoIDFK = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link it.apuliait.model.impl.LineaFatturaModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
	 * </p>
	 *
	 * @param documentoIDFK the documento i d f k
	 * @param start the lower bound of the range of linea fatturas
	 * @param end the upper bound of the range of linea fatturas (not inclusive)
	 * @return the range of matching linea fatturas
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public List<LineaFattura> findByCompetenze(long documentoIDFK, int start,
		int end) throws SystemException {
		return findByCompetenze(documentoIDFK, start, end, null);
	}

	/**
	 * Returns an ordered range of all the linea fatturas where documentoIDFK = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link it.apuliait.model.impl.LineaFatturaModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
	 * </p>
	 *
	 * @param documentoIDFK the documento i d f k
	 * @param start the lower bound of the range of linea fatturas
	 * @param end the upper bound of the range of linea fatturas (not inclusive)
	 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
	 * @return the ordered range of matching linea fatturas
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public List<LineaFattura> findByCompetenze(long documentoIDFK, int start,
		int end, OrderByComparator orderByComparator) throws SystemException {
		boolean pagination = true;
		FinderPath finderPath = null;
		Object[] finderArgs = null;

		if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
				(orderByComparator == null)) {
			pagination = false;
			finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPETENZE;
			finderArgs = new Object[] { documentoIDFK };
		}
		else {
			finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_COMPETENZE;
			finderArgs = new Object[] {
					documentoIDFK,
					
					start, end, orderByComparator
				};
		}

		List<LineaFattura> list = (List<LineaFattura>)FinderCacheUtil.getResult(finderPath,
				finderArgs, this);

		if ((list != null) && !list.isEmpty()) {
			for (LineaFattura lineaFattura : list) {
				if ((documentoIDFK != lineaFattura.getDocumentoIDFK())) {
					list = null;

					break;
				}
			}
		}

		if (list == null) {
			StringBundler query = null;

			if (orderByComparator != null) {
				query = new StringBundler(3 +
						(orderByComparator.getOrderByFields().length * 3));
			}
			else {
				query = new StringBundler(3);
			}

			query.append(_SQL_SELECT_LINEAFATTURA_WHERE);

			query.append(_FINDER_COLUMN_COMPETENZE_DOCUMENTOIDFK_2);

			if (orderByComparator != null) {
				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);
			}
			else
			 if (pagination) {
				query.append(LineaFatturaModelImpl.ORDER_BY_JPQL);
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(documentoIDFK);

				if (!pagination) {
					list = (List<LineaFattura>)QueryUtil.list(q, getDialect(),
							start, end, false);

					Collections.sort(list);

					list = new UnmodifiableList<LineaFattura>(list);
				}
				else {
					list = (List<LineaFattura>)QueryUtil.list(q, getDialect(),
							start, end);
				}

				cacheResult(list);

				FinderCacheUtil.putResult(finderPath, finderArgs, list);
			}
			catch (Exception e) {
				FinderCacheUtil.removeResult(finderPath, finderArgs);

				throw processException(e);
			}
			finally {
				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Returns the first linea fattura in the ordered set where documentoIDFK = &#63;.
	 *
	 * @param documentoIDFK the documento i d f k
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the first matching linea fattura
	 * @throws it.apuliait.NoSuchLineaFatturaException if a matching linea fattura could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public LineaFattura findByCompetenze_First(long documentoIDFK,
		OrderByComparator orderByComparator)
		throws NoSuchLineaFatturaException, SystemException {
		LineaFattura lineaFattura = fetchByCompetenze_First(documentoIDFK,
				orderByComparator);

		if (lineaFattura != null) {
			return lineaFattura;
		}

		StringBundler msg = new StringBundler(4);

		msg.append(_NO_SUCH_ENTITY_WITH_KEY);

		msg.append("documentoIDFK=");
		msg.append(documentoIDFK);

		msg.append(StringPool.CLOSE_CURLY_BRACE);

		throw new NoSuchLineaFatturaException(msg.toString());
	}

	/**
	 * Returns the first linea fattura in the ordered set where documentoIDFK = &#63;.
	 *
	 * @param documentoIDFK the documento i d f k
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the first matching linea fattura, or <code>null</code> if a matching linea fattura could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public LineaFattura fetchByCompetenze_First(long documentoIDFK,
		OrderByComparator orderByComparator) throws SystemException {
		List<LineaFattura> list = findByCompetenze(documentoIDFK, 0, 1,
				orderByComparator);

		if (!list.isEmpty()) {
			return list.get(0);
		}

		return null;
	}

	/**
	 * Returns the last linea fattura in the ordered set where documentoIDFK = &#63;.
	 *
	 * @param documentoIDFK the documento i d f k
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the last matching linea fattura
	 * @throws it.apuliait.NoSuchLineaFatturaException if a matching linea fattura could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public LineaFattura findByCompetenze_Last(long documentoIDFK,
		OrderByComparator orderByComparator)
		throws NoSuchLineaFatturaException, SystemException {
		LineaFattura lineaFattura = fetchByCompetenze_Last(documentoIDFK,
				orderByComparator);

		if (lineaFattura != null) {
			return lineaFattura;
		}

		StringBundler msg = new StringBundler(4);

		msg.append(_NO_SUCH_ENTITY_WITH_KEY);

		msg.append("documentoIDFK=");
		msg.append(documentoIDFK);

		msg.append(StringPool.CLOSE_CURLY_BRACE);

		throw new NoSuchLineaFatturaException(msg.toString());
	}

	/**
	 * Returns the last linea fattura in the ordered set where documentoIDFK = &#63;.
	 *
	 * @param documentoIDFK the documento i d f k
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the last matching linea fattura, or <code>null</code> if a matching linea fattura could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public LineaFattura fetchByCompetenze_Last(long documentoIDFK,
		OrderByComparator orderByComparator) throws SystemException {
		int count = countByCompetenze(documentoIDFK);

		if (count == 0) {
			return null;
		}

		List<LineaFattura> list = findByCompetenze(documentoIDFK, count - 1,
				count, orderByComparator);

		if (!list.isEmpty()) {
			return list.get(0);
		}

		return null;
	}

	/**
	 * Returns the linea fatturas before and after the current linea fattura in the ordered set where documentoIDFK = &#63;.
	 *
	 * @param lineaFatturaId the primary key of the current linea fattura
	 * @param documentoIDFK the documento i d f k
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the previous, current, and next linea fattura
	 * @throws it.apuliait.NoSuchLineaFatturaException if a linea fattura with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public LineaFattura[] findByCompetenze_PrevAndNext(long lineaFatturaId,
		long documentoIDFK, OrderByComparator orderByComparator)
		throws NoSuchLineaFatturaException, SystemException {
		LineaFattura lineaFattura = findByPrimaryKey(lineaFatturaId);

		Session session = null;

		try {
			session = openSession();

			LineaFattura[] array = new LineaFatturaImpl[3];

			array[0] = getByCompetenze_PrevAndNext(session, lineaFattura,
					documentoIDFK, orderByComparator, true);

			array[1] = lineaFattura;

			array[2] = getByCompetenze_PrevAndNext(session, lineaFattura,
					documentoIDFK, orderByComparator, false);

			return array;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected LineaFattura getByCompetenze_PrevAndNext(Session session,
		LineaFattura lineaFattura, long documentoIDFK,
		OrderByComparator orderByComparator, boolean previous) {
		StringBundler query = null;

		if (orderByComparator != null) {
			query = new StringBundler(6 +
					(orderByComparator.getOrderByFields().length * 6));
		}
		else {
			query = new StringBundler(3);
		}

		query.append(_SQL_SELECT_LINEAFATTURA_WHERE);

		query.append(_FINDER_COLUMN_COMPETENZE_DOCUMENTOIDFK_2);

		if (orderByComparator != null) {
			String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

			if (orderByConditionFields.length > 0) {
				query.append(WHERE_AND);
			}

			for (int i = 0; i < orderByConditionFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByConditionFields[i]);

				if ((i + 1) < orderByConditionFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN_HAS_NEXT);
					}
					else {
						query.append(WHERE_LESSER_THAN_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN);
					}
					else {
						query.append(WHERE_LESSER_THAN);
					}
				}
			}

			query.append(ORDER_BY_CLAUSE);

			String[] orderByFields = orderByComparator.getOrderByFields();

			for (int i = 0; i < orderByFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByFields[i]);

				if ((i + 1) < orderByFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC_HAS_NEXT);
					}
					else {
						query.append(ORDER_BY_DESC_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC);
					}
					else {
						query.append(ORDER_BY_DESC);
					}
				}
			}
		}
		else {
			query.append(LineaFatturaModelImpl.ORDER_BY_JPQL);
		}

		String sql = query.toString();

		Query q = session.createQuery(sql);

		q.setFirstResult(0);
		q.setMaxResults(2);

		QueryPos qPos = QueryPos.getInstance(q);

		qPos.add(documentoIDFK);

		if (orderByComparator != null) {
			Object[] values = orderByComparator.getOrderByConditionValues(lineaFattura);

			for (Object value : values) {
				qPos.add(value);
			}
		}

		List<LineaFattura> list = q.list();

		if (list.size() == 2) {
			return list.get(1);
		}
		else {
			return null;
		}
	}

	/**
	 * Removes all the linea fatturas where documentoIDFK = &#63; from the database.
	 *
	 * @param documentoIDFK the documento i d f k
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public void removeByCompetenze(long documentoIDFK)
		throws SystemException {
		for (LineaFattura lineaFattura : findByCompetenze(documentoIDFK,
				QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
			remove(lineaFattura);
		}
	}

	/**
	 * Returns the number of linea fatturas where documentoIDFK = &#63;.
	 *
	 * @param documentoIDFK the documento i d f k
	 * @return the number of matching linea fatturas
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public int countByCompetenze(long documentoIDFK) throws SystemException {
		FinderPath finderPath = FINDER_PATH_COUNT_BY_COMPETENZE;

		Object[] finderArgs = new Object[] { documentoIDFK };

		Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
				this);

		if (count == null) {
			StringBundler query = new StringBundler(2);

			query.append(_SQL_COUNT_LINEAFATTURA_WHERE);

			query.append(_FINDER_COLUMN_COMPETENZE_DOCUMENTOIDFK_2);

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(documentoIDFK);

				count = (Long)q.uniqueResult();

				FinderCacheUtil.putResult(finderPath, finderArgs, count);
			}
			catch (Exception e) {
				FinderCacheUtil.removeResult(finderPath, finderArgs);

				throw processException(e);
			}
			finally {
				closeSession(session);
			}
		}

		return count.intValue();
	}

	private static final String _FINDER_COLUMN_COMPETENZE_DOCUMENTOIDFK_2 = "lineaFattura.documentoIDFK = ?";
	public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_COMPETENZEFORDDT =
		new FinderPath(LineaFatturaModelImpl.ENTITY_CACHE_ENABLED,
			LineaFatturaModelImpl.FINDER_CACHE_ENABLED, LineaFatturaImpl.class,
			FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByCompetenzeForDDT",
			new String[] {
				Long.class.getName(), String.class.getName(),
				String.class.getName(),
				
			Integer.class.getName(), Integer.class.getName(),
				OrderByComparator.class.getName()
			});
	public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPETENZEFORDDT =
		new FinderPath(LineaFatturaModelImpl.ENTITY_CACHE_ENABLED,
			LineaFatturaModelImpl.FINDER_CACHE_ENABLED, LineaFatturaImpl.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
			"findByCompetenzeForDDT",
			new String[] {
				Long.class.getName(), String.class.getName(),
				String.class.getName()
			},
			LineaFatturaModelImpl.DOCUMENTOIDFK_COLUMN_BITMASK |
			LineaFatturaModelImpl.NUMERODDT_COLUMN_BITMASK |
			LineaFatturaModelImpl.DATADDT_COLUMN_BITMASK);
	public static final FinderPath FINDER_PATH_COUNT_BY_COMPETENZEFORDDT = new FinderPath(LineaFatturaModelImpl.ENTITY_CACHE_ENABLED,
			LineaFatturaModelImpl.FINDER_CACHE_ENABLED, Long.class,
			FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
			"countByCompetenzeForDDT",
			new String[] {
				Long.class.getName(), String.class.getName(),
				String.class.getName()
			});

	/**
	 * Returns all the linea fatturas where documentoIDFK = &#63; and numeroDdt = &#63; and dataDdt = &#63;.
	 *
	 * @param documentoIDFK the documento i d f k
	 * @param numeroDdt the numero ddt
	 * @param dataDdt the data ddt
	 * @return the matching linea fatturas
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public List<LineaFattura> findByCompetenzeForDDT(long documentoIDFK,
		String numeroDdt, String dataDdt) throws SystemException {
		return findByCompetenzeForDDT(documentoIDFK, numeroDdt, dataDdt,
			QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Returns a range of all the linea fatturas where documentoIDFK = &#63; and numeroDdt = &#63; and dataDdt = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link it.apuliait.model.impl.LineaFatturaModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
	 * </p>
	 *
	 * @param documentoIDFK the documento i d f k
	 * @param numeroDdt the numero ddt
	 * @param dataDdt the data ddt
	 * @param start the lower bound of the range of linea fatturas
	 * @param end the upper bound of the range of linea fatturas (not inclusive)
	 * @return the range of matching linea fatturas
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public List<LineaFattura> findByCompetenzeForDDT(long documentoIDFK,
		String numeroDdt, String dataDdt, int start, int end)
		throws SystemException {
		return findByCompetenzeForDDT(documentoIDFK, numeroDdt, dataDdt, start,
			end, null);
	}

	/**
	 * Returns an ordered range of all the linea fatturas where documentoIDFK = &#63; and numeroDdt = &#63; and dataDdt = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link it.apuliait.model.impl.LineaFatturaModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
	 * </p>
	 *
	 * @param documentoIDFK the documento i d f k
	 * @param numeroDdt the numero ddt
	 * @param dataDdt the data ddt
	 * @param start the lower bound of the range of linea fatturas
	 * @param end the upper bound of the range of linea fatturas (not inclusive)
	 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
	 * @return the ordered range of matching linea fatturas
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public List<LineaFattura> findByCompetenzeForDDT(long documentoIDFK,
		String numeroDdt, String dataDdt, int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		boolean pagination = true;
		FinderPath finderPath = null;
		Object[] finderArgs = null;

		if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
				(orderByComparator == null)) {
			pagination = false;
			finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPETENZEFORDDT;
			finderArgs = new Object[] { documentoIDFK, numeroDdt, dataDdt };
		}
		else {
			finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_COMPETENZEFORDDT;
			finderArgs = new Object[] {
					documentoIDFK, numeroDdt, dataDdt,
					
					start, end, orderByComparator
				};
		}

		List<LineaFattura> list = (List<LineaFattura>)FinderCacheUtil.getResult(finderPath,
				finderArgs, this);

		if ((list != null) && !list.isEmpty()) {
			for (LineaFattura lineaFattura : list) {
				if ((documentoIDFK != lineaFattura.getDocumentoIDFK()) ||
						!Validator.equals(numeroDdt, lineaFattura.getNumeroDdt()) ||
						!Validator.equals(dataDdt, lineaFattura.getDataDdt())) {
					list = null;

					break;
				}
			}
		}

		if (list == null) {
			StringBundler query = null;

			if (orderByComparator != null) {
				query = new StringBundler(5 +
						(orderByComparator.getOrderByFields().length * 3));
			}
			else {
				query = new StringBundler(5);
			}

			query.append(_SQL_SELECT_LINEAFATTURA_WHERE);

			query.append(_FINDER_COLUMN_COMPETENZEFORDDT_DOCUMENTOIDFK_2);

			boolean bindNumeroDdt = false;

			if (numeroDdt == null) {
				query.append(_FINDER_COLUMN_COMPETENZEFORDDT_NUMERODDT_1);
			}
			else if (numeroDdt.equals(StringPool.BLANK)) {
				query.append(_FINDER_COLUMN_COMPETENZEFORDDT_NUMERODDT_3);
			}
			else {
				bindNumeroDdt = true;

				query.append(_FINDER_COLUMN_COMPETENZEFORDDT_NUMERODDT_2);
			}

			boolean bindDataDdt = false;

			if (dataDdt == null) {
				query.append(_FINDER_COLUMN_COMPETENZEFORDDT_DATADDT_1);
			}
			else if (dataDdt.equals(StringPool.BLANK)) {
				query.append(_FINDER_COLUMN_COMPETENZEFORDDT_DATADDT_3);
			}
			else {
				bindDataDdt = true;

				query.append(_FINDER_COLUMN_COMPETENZEFORDDT_DATADDT_2);
			}

			if (orderByComparator != null) {
				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);
			}
			else
			 if (pagination) {
				query.append(LineaFatturaModelImpl.ORDER_BY_JPQL);
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(documentoIDFK);

				if (bindNumeroDdt) {
					qPos.add(numeroDdt);
				}

				if (bindDataDdt) {
					qPos.add(dataDdt);
				}

				if (!pagination) {
					list = (List<LineaFattura>)QueryUtil.list(q, getDialect(),
							start, end, false);

					Collections.sort(list);

					list = new UnmodifiableList<LineaFattura>(list);
				}
				else {
					list = (List<LineaFattura>)QueryUtil.list(q, getDialect(),
							start, end);
				}

				cacheResult(list);

				FinderCacheUtil.putResult(finderPath, finderArgs, list);
			}
			catch (Exception e) {
				FinderCacheUtil.removeResult(finderPath, finderArgs);

				throw processException(e);
			}
			finally {
				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Returns the first linea fattura in the ordered set where documentoIDFK = &#63; and numeroDdt = &#63; and dataDdt = &#63;.
	 *
	 * @param documentoIDFK the documento i d f k
	 * @param numeroDdt the numero ddt
	 * @param dataDdt the data ddt
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the first matching linea fattura
	 * @throws it.apuliait.NoSuchLineaFatturaException if a matching linea fattura could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public LineaFattura findByCompetenzeForDDT_First(long documentoIDFK,
		String numeroDdt, String dataDdt, OrderByComparator orderByComparator)
		throws NoSuchLineaFatturaException, SystemException {
		LineaFattura lineaFattura = fetchByCompetenzeForDDT_First(documentoIDFK,
				numeroDdt, dataDdt, orderByComparator);

		if (lineaFattura != null) {
			return lineaFattura;
		}

		StringBundler msg = new StringBundler(8);

		msg.append(_NO_SUCH_ENTITY_WITH_KEY);

		msg.append("documentoIDFK=");
		msg.append(documentoIDFK);

		msg.append(", numeroDdt=");
		msg.append(numeroDdt);

		msg.append(", dataDdt=");
		msg.append(dataDdt);

		msg.append(StringPool.CLOSE_CURLY_BRACE);

		throw new NoSuchLineaFatturaException(msg.toString());
	}

	/**
	 * Returns the first linea fattura in the ordered set where documentoIDFK = &#63; and numeroDdt = &#63; and dataDdt = &#63;.
	 *
	 * @param documentoIDFK the documento i d f k
	 * @param numeroDdt the numero ddt
	 * @param dataDdt the data ddt
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the first matching linea fattura, or <code>null</code> if a matching linea fattura could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public LineaFattura fetchByCompetenzeForDDT_First(long documentoIDFK,
		String numeroDdt, String dataDdt, OrderByComparator orderByComparator)
		throws SystemException {
		List<LineaFattura> list = findByCompetenzeForDDT(documentoIDFK,
				numeroDdt, dataDdt, 0, 1, orderByComparator);

		if (!list.isEmpty()) {
			return list.get(0);
		}

		return null;
	}

	/**
	 * Returns the last linea fattura in the ordered set where documentoIDFK = &#63; and numeroDdt = &#63; and dataDdt = &#63;.
	 *
	 * @param documentoIDFK the documento i d f k
	 * @param numeroDdt the numero ddt
	 * @param dataDdt the data ddt
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the last matching linea fattura
	 * @throws it.apuliait.NoSuchLineaFatturaException if a matching linea fattura could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public LineaFattura findByCompetenzeForDDT_Last(long documentoIDFK,
		String numeroDdt, String dataDdt, OrderByComparator orderByComparator)
		throws NoSuchLineaFatturaException, SystemException {
		LineaFattura lineaFattura = fetchByCompetenzeForDDT_Last(documentoIDFK,
				numeroDdt, dataDdt, orderByComparator);

		if (lineaFattura != null) {
			return lineaFattura;
		}

		StringBundler msg = new StringBundler(8);

		msg.append(_NO_SUCH_ENTITY_WITH_KEY);

		msg.append("documentoIDFK=");
		msg.append(documentoIDFK);

		msg.append(", numeroDdt=");
		msg.append(numeroDdt);

		msg.append(", dataDdt=");
		msg.append(dataDdt);

		msg.append(StringPool.CLOSE_CURLY_BRACE);

		throw new NoSuchLineaFatturaException(msg.toString());
	}

	/**
	 * Returns the last linea fattura in the ordered set where documentoIDFK = &#63; and numeroDdt = &#63; and dataDdt = &#63;.
	 *
	 * @param documentoIDFK the documento i d f k
	 * @param numeroDdt the numero ddt
	 * @param dataDdt the data ddt
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the last matching linea fattura, or <code>null</code> if a matching linea fattura could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public LineaFattura fetchByCompetenzeForDDT_Last(long documentoIDFK,
		String numeroDdt, String dataDdt, OrderByComparator orderByComparator)
		throws SystemException {
		int count = countByCompetenzeForDDT(documentoIDFK, numeroDdt, dataDdt);

		if (count == 0) {
			return null;
		}

		List<LineaFattura> list = findByCompetenzeForDDT(documentoIDFK,
				numeroDdt, dataDdt, count - 1, count, orderByComparator);

		if (!list.isEmpty()) {
			return list.get(0);
		}

		return null;
	}

	/**
	 * Returns the linea fatturas before and after the current linea fattura in the ordered set where documentoIDFK = &#63; and numeroDdt = &#63; and dataDdt = &#63;.
	 *
	 * @param lineaFatturaId the primary key of the current linea fattura
	 * @param documentoIDFK the documento i d f k
	 * @param numeroDdt the numero ddt
	 * @param dataDdt the data ddt
	 * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
	 * @return the previous, current, and next linea fattura
	 * @throws it.apuliait.NoSuchLineaFatturaException if a linea fattura with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public LineaFattura[] findByCompetenzeForDDT_PrevAndNext(
		long lineaFatturaId, long documentoIDFK, String numeroDdt,
		String dataDdt, OrderByComparator orderByComparator)
		throws NoSuchLineaFatturaException, SystemException {
		LineaFattura lineaFattura = findByPrimaryKey(lineaFatturaId);

		Session session = null;

		try {
			session = openSession();

			LineaFattura[] array = new LineaFatturaImpl[3];

			array[0] = getByCompetenzeForDDT_PrevAndNext(session, lineaFattura,
					documentoIDFK, numeroDdt, dataDdt, orderByComparator, true);

			array[1] = lineaFattura;

			array[2] = getByCompetenzeForDDT_PrevAndNext(session, lineaFattura,
					documentoIDFK, numeroDdt, dataDdt, orderByComparator, false);

			return array;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected LineaFattura getByCompetenzeForDDT_PrevAndNext(Session session,
		LineaFattura lineaFattura, long documentoIDFK, String numeroDdt,
		String dataDdt, OrderByComparator orderByComparator, boolean previous) {
		StringBundler query = null;

		if (orderByComparator != null) {
			query = new StringBundler(6 +
					(orderByComparator.getOrderByFields().length * 6));
		}
		else {
			query = new StringBundler(3);
		}

		query.append(_SQL_SELECT_LINEAFATTURA_WHERE);

		query.append(_FINDER_COLUMN_COMPETENZEFORDDT_DOCUMENTOIDFK_2);

		boolean bindNumeroDdt = false;

		if (numeroDdt == null) {
			query.append(_FINDER_COLUMN_COMPETENZEFORDDT_NUMERODDT_1);
		}
		else if (numeroDdt.equals(StringPool.BLANK)) {
			query.append(_FINDER_COLUMN_COMPETENZEFORDDT_NUMERODDT_3);
		}
		else {
			bindNumeroDdt = true;

			query.append(_FINDER_COLUMN_COMPETENZEFORDDT_NUMERODDT_2);
		}

		boolean bindDataDdt = false;

		if (dataDdt == null) {
			query.append(_FINDER_COLUMN_COMPETENZEFORDDT_DATADDT_1);
		}
		else if (dataDdt.equals(StringPool.BLANK)) {
			query.append(_FINDER_COLUMN_COMPETENZEFORDDT_DATADDT_3);
		}
		else {
			bindDataDdt = true;

			query.append(_FINDER_COLUMN_COMPETENZEFORDDT_DATADDT_2);
		}

		if (orderByComparator != null) {
			String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

			if (orderByConditionFields.length > 0) {
				query.append(WHERE_AND);
			}

			for (int i = 0; i < orderByConditionFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByConditionFields[i]);

				if ((i + 1) < orderByConditionFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN_HAS_NEXT);
					}
					else {
						query.append(WHERE_LESSER_THAN_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN);
					}
					else {
						query.append(WHERE_LESSER_THAN);
					}
				}
			}

			query.append(ORDER_BY_CLAUSE);

			String[] orderByFields = orderByComparator.getOrderByFields();

			for (int i = 0; i < orderByFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByFields[i]);

				if ((i + 1) < orderByFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC_HAS_NEXT);
					}
					else {
						query.append(ORDER_BY_DESC_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC);
					}
					else {
						query.append(ORDER_BY_DESC);
					}
				}
			}
		}
		else {
			query.append(LineaFatturaModelImpl.ORDER_BY_JPQL);
		}

		String sql = query.toString();

		Query q = session.createQuery(sql);

		q.setFirstResult(0);
		q.setMaxResults(2);

		QueryPos qPos = QueryPos.getInstance(q);

		qPos.add(documentoIDFK);

		if (bindNumeroDdt) {
			qPos.add(numeroDdt);
		}

		if (bindDataDdt) {
			qPos.add(dataDdt);
		}

		if (orderByComparator != null) {
			Object[] values = orderByComparator.getOrderByConditionValues(lineaFattura);

			for (Object value : values) {
				qPos.add(value);
			}
		}

		List<LineaFattura> list = q.list();

		if (list.size() == 2) {
			return list.get(1);
		}
		else {
			return null;
		}
	}

	/**
	 * Removes all the linea fatturas where documentoIDFK = &#63; and numeroDdt = &#63; and dataDdt = &#63; from the database.
	 *
	 * @param documentoIDFK the documento i d f k
	 * @param numeroDdt the numero ddt
	 * @param dataDdt the data ddt
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public void removeByCompetenzeForDDT(long documentoIDFK, String numeroDdt,
		String dataDdt) throws SystemException {
		for (LineaFattura lineaFattura : findByCompetenzeForDDT(documentoIDFK,
				numeroDdt, dataDdt, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
			remove(lineaFattura);
		}
	}

	/**
	 * Returns the number of linea fatturas where documentoIDFK = &#63; and numeroDdt = &#63; and dataDdt = &#63;.
	 *
	 * @param documentoIDFK the documento i d f k
	 * @param numeroDdt the numero ddt
	 * @param dataDdt the data ddt
	 * @return the number of matching linea fatturas
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public int countByCompetenzeForDDT(long documentoIDFK, String numeroDdt,
		String dataDdt) throws SystemException {
		FinderPath finderPath = FINDER_PATH_COUNT_BY_COMPETENZEFORDDT;

		Object[] finderArgs = new Object[] { documentoIDFK, numeroDdt, dataDdt };

		Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
				this);

		if (count == null) {
			StringBundler query = new StringBundler(4);

			query.append(_SQL_COUNT_LINEAFATTURA_WHERE);

			query.append(_FINDER_COLUMN_COMPETENZEFORDDT_DOCUMENTOIDFK_2);

			boolean bindNumeroDdt = false;

			if (numeroDdt == null) {
				query.append(_FINDER_COLUMN_COMPETENZEFORDDT_NUMERODDT_1);
			}
			else if (numeroDdt.equals(StringPool.BLANK)) {
				query.append(_FINDER_COLUMN_COMPETENZEFORDDT_NUMERODDT_3);
			}
			else {
				bindNumeroDdt = true;

				query.append(_FINDER_COLUMN_COMPETENZEFORDDT_NUMERODDT_2);
			}

			boolean bindDataDdt = false;

			if (dataDdt == null) {
				query.append(_FINDER_COLUMN_COMPETENZEFORDDT_DATADDT_1);
			}
			else if (dataDdt.equals(StringPool.BLANK)) {
				query.append(_FINDER_COLUMN_COMPETENZEFORDDT_DATADDT_3);
			}
			else {
				bindDataDdt = true;

				query.append(_FINDER_COLUMN_COMPETENZEFORDDT_DATADDT_2);
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(documentoIDFK);

				if (bindNumeroDdt) {
					qPos.add(numeroDdt);
				}

				if (bindDataDdt) {
					qPos.add(dataDdt);
				}

				count = (Long)q.uniqueResult();

				FinderCacheUtil.putResult(finderPath, finderArgs, count);
			}
			catch (Exception e) {
				FinderCacheUtil.removeResult(finderPath, finderArgs);

				throw processException(e);
			}
			finally {
				closeSession(session);
			}
		}

		return count.intValue();
	}

	private static final String _FINDER_COLUMN_COMPETENZEFORDDT_DOCUMENTOIDFK_2 = "lineaFattura.documentoIDFK = ? AND ";
	private static final String _FINDER_COLUMN_COMPETENZEFORDDT_NUMERODDT_1 = "lineaFattura.numeroDdt IS NULL AND ";
	private static final String _FINDER_COLUMN_COMPETENZEFORDDT_NUMERODDT_2 = "lineaFattura.numeroDdt = ? AND ";
	private static final String _FINDER_COLUMN_COMPETENZEFORDDT_NUMERODDT_3 = "(lineaFattura.numeroDdt IS NULL OR lineaFattura.numeroDdt = '') AND ";
	private static final String _FINDER_COLUMN_COMPETENZEFORDDT_DATADDT_1 = "lineaFattura.dataDdt IS NULL";
	private static final String _FINDER_COLUMN_COMPETENZEFORDDT_DATADDT_2 = "lineaFattura.dataDdt = ?";
	private static final String _FINDER_COLUMN_COMPETENZEFORDDT_DATADDT_3 = "(lineaFattura.dataDdt IS NULL OR lineaFattura.dataDdt = '')";

	public LineaFatturaPersistenceImpl() {
		setModelClass(LineaFattura.class);
	}

	/**
	 * Caches the linea fattura in the entity cache if it is enabled.
	 *
	 * @param lineaFattura the linea fattura
	 */
	@Override
	public void cacheResult(LineaFattura lineaFattura) {
		EntityCacheUtil.putResult(LineaFatturaModelImpl.ENTITY_CACHE_ENABLED,
			LineaFatturaImpl.class, lineaFattura.getPrimaryKey(), lineaFattura);

		lineaFattura.resetOriginalValues();
	}

	/**
	 * Caches the linea fatturas in the entity cache if it is enabled.
	 *
	 * @param lineaFatturas the linea fatturas
	 */
	@Override
	public void cacheResult(List<LineaFattura> lineaFatturas) {
		for (LineaFattura lineaFattura : lineaFatturas) {
			if (EntityCacheUtil.getResult(
						LineaFatturaModelImpl.ENTITY_CACHE_ENABLED,
						LineaFatturaImpl.class, lineaFattura.getPrimaryKey()) == null) {
				cacheResult(lineaFattura);
			}
			else {
				lineaFattura.resetOriginalValues();
			}
		}
	}

	/**
	 * Clears the cache for all linea fatturas.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	@Override
	public void clearCache() {
		if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
			CacheRegistryUtil.clear(LineaFatturaImpl.class.getName());
		}

		EntityCacheUtil.clearCache(LineaFatturaImpl.class.getName());

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
	}

	/**
	 * Clears the cache for the linea fattura.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	@Override
	public void clearCache(LineaFattura lineaFattura) {
		EntityCacheUtil.removeResult(LineaFatturaModelImpl.ENTITY_CACHE_ENABLED,
			LineaFatturaImpl.class, lineaFattura.getPrimaryKey());

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
	}

	@Override
	public void clearCache(List<LineaFattura> lineaFatturas) {
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

		for (LineaFattura lineaFattura : lineaFatturas) {
			EntityCacheUtil.removeResult(LineaFatturaModelImpl.ENTITY_CACHE_ENABLED,
				LineaFatturaImpl.class, lineaFattura.getPrimaryKey());
		}
	}

	/**
	 * Creates a new linea fattura with the primary key. Does not add the linea fattura to the database.
	 *
	 * @param lineaFatturaId the primary key for the new linea fattura
	 * @return the new linea fattura
	 */
	@Override
	public LineaFattura create(long lineaFatturaId) {
		LineaFattura lineaFattura = new LineaFatturaImpl();

		lineaFattura.setNew(true);
		lineaFattura.setPrimaryKey(lineaFatturaId);

		return lineaFattura;
	}

	/**
	 * Removes the linea fattura with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param lineaFatturaId the primary key of the linea fattura
	 * @return the linea fattura that was removed
	 * @throws it.apuliait.NoSuchLineaFatturaException if a linea fattura with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public LineaFattura remove(long lineaFatturaId)
		throws NoSuchLineaFatturaException, SystemException {
		return remove((Serializable)lineaFatturaId);
	}

	/**
	 * Removes the linea fattura with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param primaryKey the primary key of the linea fattura
	 * @return the linea fattura that was removed
	 * @throws it.apuliait.NoSuchLineaFatturaException if a linea fattura with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public LineaFattura remove(Serializable primaryKey)
		throws NoSuchLineaFatturaException, SystemException {
		Session session = null;

		try {
			session = openSession();

			LineaFattura lineaFattura = (LineaFattura)session.get(LineaFatturaImpl.class,
					primaryKey);

			if (lineaFattura == null) {
				if (_log.isWarnEnabled()) {
					_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
				}

				throw new NoSuchLineaFatturaException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
					primaryKey);
			}

			return remove(lineaFattura);
		}
		catch (NoSuchLineaFatturaException nsee) {
			throw nsee;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	@Override
	protected LineaFattura removeImpl(LineaFattura lineaFattura)
		throws SystemException {
		lineaFattura = toUnwrappedModel(lineaFattura);

		Session session = null;

		try {
			session = openSession();

			if (!session.contains(lineaFattura)) {
				lineaFattura = (LineaFattura)session.get(LineaFatturaImpl.class,
						lineaFattura.getPrimaryKeyObj());
			}

			if (lineaFattura != null) {
				session.delete(lineaFattura);
			}
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		if (lineaFattura != null) {
			clearCache(lineaFattura);
		}

		return lineaFattura;
	}

	@Override
	public LineaFattura updateImpl(it.apuliait.model.LineaFattura lineaFattura)
		throws SystemException {
		lineaFattura = toUnwrappedModel(lineaFattura);

		boolean isNew = lineaFattura.isNew();

		LineaFatturaModelImpl lineaFatturaModelImpl = (LineaFatturaModelImpl)lineaFattura;

		Session session = null;

		try {
			session = openSession();

			if (lineaFattura.isNew()) {
				session.save(lineaFattura);

				lineaFattura.setNew(false);
			}
			else {
				session.merge(lineaFattura);
			}
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

		if (isNew || !LineaFatturaModelImpl.COLUMN_BITMASK_ENABLED) {
			FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
		}

		else {
			if ((lineaFatturaModelImpl.getColumnBitmask() &
					FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPETENZE.getColumnBitmask()) != 0) {
				Object[] args = new Object[] {
						lineaFatturaModelImpl.getOriginalDocumentoIDFK()
					};

				FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_COMPETENZE,
					args);
				FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPETENZE,
					args);

				args = new Object[] { lineaFatturaModelImpl.getDocumentoIDFK() };

				FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_COMPETENZE,
					args);
				FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPETENZE,
					args);
			}

			if ((lineaFatturaModelImpl.getColumnBitmask() &
					FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPETENZEFORDDT.getColumnBitmask()) != 0) {
				Object[] args = new Object[] {
						lineaFatturaModelImpl.getOriginalDocumentoIDFK(),
						lineaFatturaModelImpl.getOriginalNumeroDdt(),
						lineaFatturaModelImpl.getOriginalDataDdt()
					};

				FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_COMPETENZEFORDDT,
					args);
				FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPETENZEFORDDT,
					args);

				args = new Object[] {
						lineaFatturaModelImpl.getDocumentoIDFK(),
						lineaFatturaModelImpl.getNumeroDdt(),
						lineaFatturaModelImpl.getDataDdt()
					};

				FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_COMPETENZEFORDDT,
					args);
				FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPETENZEFORDDT,
					args);
			}
		}

		EntityCacheUtil.putResult(LineaFatturaModelImpl.ENTITY_CACHE_ENABLED,
			LineaFatturaImpl.class, lineaFattura.getPrimaryKey(), lineaFattura);

		return lineaFattura;
	}

	protected LineaFattura toUnwrappedModel(LineaFattura lineaFattura) {
		if (lineaFattura instanceof LineaFatturaImpl) {
			return lineaFattura;
		}

		LineaFatturaImpl lineaFatturaImpl = new LineaFatturaImpl();

		lineaFatturaImpl.setNew(lineaFattura.isNew());
		lineaFatturaImpl.setPrimaryKey(lineaFattura.getPrimaryKey());

		lineaFatturaImpl.setLineaFatturaId(lineaFattura.getLineaFatturaId());
		lineaFatturaImpl.setDocumentoIDFK(lineaFattura.getDocumentoIDFK());
		lineaFatturaImpl.setNumeroLinea(lineaFattura.getNumeroLinea());
		lineaFatturaImpl.setDescrizione(lineaFattura.getDescrizione());
		lineaFatturaImpl.setDataInizio(lineaFattura.getDataInizio());
		lineaFatturaImpl.setDataFine(lineaFattura.getDataFine());
		lineaFatturaImpl.setQuantita(lineaFattura.getQuantita());
		lineaFatturaImpl.setPrezzoUnitario(lineaFattura.getPrezzoUnitario());
		lineaFatturaImpl.setPrezzoTotale(lineaFattura.getPrezzoTotale());
		lineaFatturaImpl.setAliquotaIvaLinea(lineaFattura.getAliquotaIvaLinea());
		lineaFatturaImpl.setTipoPrestazione(lineaFattura.getTipoPrestazione());
		lineaFatturaImpl.setRitenuta(lineaFattura.getRitenuta());
		lineaFatturaImpl.setNatura(lineaFattura.getNatura());
		lineaFatturaImpl.setRiferimentoAmministrativo(lineaFattura.getRiferimentoAmministrativo());
		lineaFatturaImpl.setNumeroDdt(lineaFattura.getNumeroDdt());
		lineaFatturaImpl.setDataDdt(lineaFattura.getDataDdt());
		lineaFatturaImpl.setIdDocumentoOrdine(lineaFattura.getIdDocumentoOrdine());
		lineaFatturaImpl.setDataOrdine(lineaFattura.getDataOrdine());
		lineaFatturaImpl.setCodiceCigOrdine(lineaFattura.getCodiceCigOrdine());
		lineaFatturaImpl.setCodiceCupOrdine(lineaFattura.getCodiceCupOrdine());
		lineaFatturaImpl.setIdDocumentoContratto(lineaFattura.getIdDocumentoContratto());
		lineaFatturaImpl.setDataContratto(lineaFattura.getDataContratto());
		lineaFatturaImpl.setCodiceCigContratto(lineaFattura.getCodiceCigContratto());
		lineaFatturaImpl.setCodiceCupContratto(lineaFattura.getCodiceCupContratto());
		lineaFatturaImpl.setIdDocumentoConvenzione(lineaFattura.getIdDocumentoConvenzione());
		lineaFatturaImpl.setDataConvenzione(lineaFattura.getDataConvenzione());
		lineaFatturaImpl.setCodiceCigConvenzione(lineaFattura.getCodiceCigConvenzione());
		lineaFatturaImpl.setCodiceCupConvenzione(lineaFattura.getCodiceCupConvenzione());
		lineaFatturaImpl.setIdDocumentoRicezione(lineaFattura.getIdDocumentoRicezione());
		lineaFatturaImpl.setDataRicezione(lineaFattura.getDataRicezione());
		lineaFatturaImpl.setCodiceCigRicezione(lineaFattura.getCodiceCigRicezione());
		lineaFatturaImpl.setCodiceCupRicezione(lineaFattura.getCodiceCupRicezione());

		return lineaFatturaImpl;
	}

	/**
	 * Returns the linea fattura with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
	 *
	 * @param primaryKey the primary key of the linea fattura
	 * @return the linea fattura
	 * @throws it.apuliait.NoSuchLineaFatturaException if a linea fattura with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public LineaFattura findByPrimaryKey(Serializable primaryKey)
		throws NoSuchLineaFatturaException, SystemException {
		LineaFattura lineaFattura = fetchByPrimaryKey(primaryKey);

		if (lineaFattura == null) {
			if (_log.isWarnEnabled()) {
				_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
			}

			throw new NoSuchLineaFatturaException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
				primaryKey);
		}

		return lineaFattura;
	}

	/**
	 * Returns the linea fattura with the primary key or throws a {@link it.apuliait.NoSuchLineaFatturaException} if it could not be found.
	 *
	 * @param lineaFatturaId the primary key of the linea fattura
	 * @return the linea fattura
	 * @throws it.apuliait.NoSuchLineaFatturaException if a linea fattura with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public LineaFattura findByPrimaryKey(long lineaFatturaId)
		throws NoSuchLineaFatturaException, SystemException {
		return findByPrimaryKey((Serializable)lineaFatturaId);
	}

	/**
	 * Returns the linea fattura with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param primaryKey the primary key of the linea fattura
	 * @return the linea fattura, or <code>null</code> if a linea fattura with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public LineaFattura fetchByPrimaryKey(Serializable primaryKey)
		throws SystemException {
		LineaFattura lineaFattura = (LineaFattura)EntityCacheUtil.getResult(LineaFatturaModelImpl.ENTITY_CACHE_ENABLED,
				LineaFatturaImpl.class, primaryKey);

		if (lineaFattura == _nullLineaFattura) {
			return null;
		}

		if (lineaFattura == null) {
			Session session = null;

			try {
				session = openSession();

				lineaFattura = (LineaFattura)session.get(LineaFatturaImpl.class,
						primaryKey);

				if (lineaFattura != null) {
					cacheResult(lineaFattura);
				}
				else {
					EntityCacheUtil.putResult(LineaFatturaModelImpl.ENTITY_CACHE_ENABLED,
						LineaFatturaImpl.class, primaryKey, _nullLineaFattura);
				}
			}
			catch (Exception e) {
				EntityCacheUtil.removeResult(LineaFatturaModelImpl.ENTITY_CACHE_ENABLED,
					LineaFatturaImpl.class, primaryKey);

				throw processException(e);
			}
			finally {
				closeSession(session);
			}
		}

		return lineaFattura;
	}

	/**
	 * Returns the linea fattura with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param lineaFatturaId the primary key of the linea fattura
	 * @return the linea fattura, or <code>null</code> if a linea fattura with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public LineaFattura fetchByPrimaryKey(long lineaFatturaId)
		throws SystemException {
		return fetchByPrimaryKey((Serializable)lineaFatturaId);
	}

	/**
	 * Returns all the linea fatturas.
	 *
	 * @return the linea fatturas
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public List<LineaFattura> findAll() throws SystemException {
		return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Returns a range of all the linea fatturas.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link it.apuliait.model.impl.LineaFatturaModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
	 * </p>
	 *
	 * @param start the lower bound of the range of linea fatturas
	 * @param end the upper bound of the range of linea fatturas (not inclusive)
	 * @return the range of linea fatturas
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public List<LineaFattura> findAll(int start, int end)
		throws SystemException {
		return findAll(start, end, null);
	}

	/**
	 * Returns an ordered range of all the linea fatturas.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link it.apuliait.model.impl.LineaFatturaModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
	 * </p>
	 *
	 * @param start the lower bound of the range of linea fatturas
	 * @param end the upper bound of the range of linea fatturas (not inclusive)
	 * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
	 * @return the ordered range of linea fatturas
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public List<LineaFattura> findAll(int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		boolean pagination = true;
		FinderPath finderPath = null;
		Object[] finderArgs = null;

		if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
				(orderByComparator == null)) {
			pagination = false;
			finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
			finderArgs = FINDER_ARGS_EMPTY;
		}
		else {
			finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
			finderArgs = new Object[] { start, end, orderByComparator };
		}

		List<LineaFattura> list = (List<LineaFattura>)FinderCacheUtil.getResult(finderPath,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;
			String sql = null;

			if (orderByComparator != null) {
				query = new StringBundler(2 +
						(orderByComparator.getOrderByFields().length * 3));

				query.append(_SQL_SELECT_LINEAFATTURA);

				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);

				sql = query.toString();
			}
			else {
				sql = _SQL_SELECT_LINEAFATTURA;

				if (pagination) {
					sql = sql.concat(LineaFatturaModelImpl.ORDER_BY_JPQL);
				}
			}

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				if (!pagination) {
					list = (List<LineaFattura>)QueryUtil.list(q, getDialect(),
							start, end, false);

					Collections.sort(list);

					list = new UnmodifiableList<LineaFattura>(list);
				}
				else {
					list = (List<LineaFattura>)QueryUtil.list(q, getDialect(),
							start, end);
				}

				cacheResult(list);

				FinderCacheUtil.putResult(finderPath, finderArgs, list);
			}
			catch (Exception e) {
				FinderCacheUtil.removeResult(finderPath, finderArgs);

				throw processException(e);
			}
			finally {
				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Removes all the linea fatturas from the database.
	 *
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public void removeAll() throws SystemException {
		for (LineaFattura lineaFattura : findAll()) {
			remove(lineaFattura);
		}
	}

	/**
	 * Returns the number of linea fatturas.
	 *
	 * @return the number of linea fatturas
	 * @throws SystemException if a system exception occurred
	 */
	@Override
	public int countAll() throws SystemException {
		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
				FINDER_ARGS_EMPTY, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(_SQL_COUNT_LINEAFATTURA);

				count = (Long)q.uniqueResult();

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
					FINDER_ARGS_EMPTY, count);
			}
			catch (Exception e) {
				FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
					FINDER_ARGS_EMPTY);

				throw processException(e);
			}
			finally {
				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Initializes the linea fattura persistence.
	 */
	public void afterPropertiesSet() {
		String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
					com.liferay.util.service.ServiceProps.get(
						"value.object.listener.it.apuliait.model.LineaFattura")));

		if (listenerClassNames.length > 0) {
			try {
				List<ModelListener<LineaFattura>> listenersList = new ArrayList<ModelListener<LineaFattura>>();

				for (String listenerClassName : listenerClassNames) {
					listenersList.add((ModelListener<LineaFattura>)InstanceFactory.newInstance(
							getClassLoader(), listenerClassName));
				}

				listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
			}
			catch (Exception e) {
				_log.error(e);
			}
		}
	}

	public void destroy() {
		EntityCacheUtil.removeCache(LineaFatturaImpl.class.getName());
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
	}

	private static final String _SQL_SELECT_LINEAFATTURA = "SELECT lineaFattura FROM LineaFattura lineaFattura";
	private static final String _SQL_SELECT_LINEAFATTURA_WHERE = "SELECT lineaFattura FROM LineaFattura lineaFattura WHERE ";
	private static final String _SQL_COUNT_LINEAFATTURA = "SELECT COUNT(lineaFattura) FROM LineaFattura lineaFattura";
	private static final String _SQL_COUNT_LINEAFATTURA_WHERE = "SELECT COUNT(lineaFattura) FROM LineaFattura lineaFattura WHERE ";
	private static final String _ORDER_BY_ENTITY_ALIAS = "lineaFattura.";
	private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No LineaFattura exists with the primary key ";
	private static final String _NO_SUCH_ENTITY_WITH_KEY = "No LineaFattura exists with the key {";
	private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = GetterUtil.getBoolean(PropsUtil.get(
				PropsKeys.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE));
	private static Log _log = LogFactoryUtil.getLog(LineaFatturaPersistenceImpl.class);
	private static LineaFattura _nullLineaFattura = new LineaFatturaImpl() {
			@Override
			public Object clone() {
				return this;
			}

			@Override
			public CacheModel<LineaFattura> toCacheModel() {
				return _nullLineaFatturaCacheModel;
			}
		};

	private static CacheModel<LineaFattura> _nullLineaFatturaCacheModel = new CacheModel<LineaFattura>() {
			@Override
			public LineaFattura toEntityModel() {
				return _nullLineaFattura;
			}
		};
}